import { _decorator, Component, Label, Node, Sprite } from 'cc';
import { BasePanel } from './BasePanel';
import { _item, _platform, _player, _resource } from '../Main';
import { BaseItem } from '../items/BaseItem';
import { Coin } from '../items/common/Coin';
import { TT } from '../platforms/minis/TT';
import { Adsdk } from '../scenes/Adsdk';
const { ccclass, property } = _decorator;

@ccclass('DailyPanel')
export class DailyPanel extends BasePanel {
    
    panelName: string = '每日奖励'

    @property(Node)
    shareNode: Node = null!

    @property(Node)
    gotNode: Node = null!

    item: BaseItem = null

    amount: number = 50

    @property(Label)
    itemName: Label = null!

    @property(Sprite)
    itemIcon: Sprite = null!

    @property(Label)
    itemAmount: Label = null!

    start() {
        this.refreshButtons()
        Adsdk.instance.showInterstitialAd()
    }

    init(item: BaseItem, amount: number = 1) {
        this.item = item
        this.amount = amount
        this.itemName.string = this.item.showName
        if (!(item instanceof Coin)) {
            this.itemIcon.spriteFrame = this.item.icon
            _resource.spriteFitSquare(this.itemIcon, 120, 120)
        }
        this.itemAmount.string = '×' + this.amount
        this.show()
    }

    refreshButtons() {
        let got: boolean = _player.daily.daily > 0 ? false : true
        this.shareNode.active = !got
        this.gotNode.active = got
    }

    share() {
        _platform.platform.share((success) => {
            if (success) {
                _player.changeNumData('daily', _player.daily, -1, 'add', true, () => {
                    this.refreshButtons()
                    _player.changeNumData(this.item.getIdName(), this.item.storeAt, this.amount, 'add', true, () => {

                    })
                })
                let pf = _platform.platform
                if (pf instanceof TT) {
                    pf.reportAnalytics('Behavior', {
                        purpose: '获取',
                        type: '道具',
                        name: this.item.showName,
                        amount: this.amount,
                        source: this.panelName,
                    })
                }
            } else {

            }
        }, 'invite')
    }

    got() {
        this.close()
    }
}


